package com.mall.product.api.dao;

import com.mall.model.ProductBundleItem;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductBundleItemMapper {

    /**
     * 插入商品组合项
     */
    @Insert({
            "INSERT INTO product_bundle_item (bundle_id, product_id, quantity, create_time)",
            "VALUES (#{bundleId}, #{productId}, #{quantity}, #{createTime})"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(ProductBundleItem productBundleItem);

    /**
     * 根据组合ID查询所有商品项
     */
    @Select("SELECT * FROM product_bundle_item WHERE bundle_id = #{bundleId}")
    List<ProductBundleItem> selectByBundleId(Long bundleId);

    /**
     * 根据商品ID查询所属的组合
     */
    @Select("SELECT * FROM product_bundle_item WHERE product_id = #{productId}")
    List<ProductBundleItem> selectByProductId(Long productId);

    /**
     * 更新商品组合项数量
     */
    @Update("UPDATE product_bundle_item SET quantity = #{quantity} WHERE id = #{id}")
    int updateQuantity(@Param("id") Long id, @Param("quantity") Integer quantity);

    /**
     * 删除商品组合项
     */
    @Delete("DELETE FROM product_bundle_item WHERE id = #{id}")
    int deleteById(Long id);

    /**
     * 删除组合下的所有商品项
     */
    @Delete("DELETE FROM product_bundle_item WHERE bundle_id = #{bundleId}")
    int deleteByBundleId(Long bundleId);

    /**
     * 批量插入商品组合项
     */
    @Insert({
            "<script>",
            "INSERT INTO product_bundle_item (bundle_id, product_id, quantity, create_time) VALUES",
            "<foreach collection='items' item='item' separator=','>",
            "(#{item.bundleId}, #{item.productId}, #{item.quantity}, #{item.createTime})",
            "</foreach>",
            "</script>"
    })
    int batchInsert(@Param("items") List<ProductBundleItem> items);
}